<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\InformationReservation;
use EasyWeChat\Factory;
use EasyWeChat\Foundation\Application;
use function EasyWeChat\Kernel\Support\generate_sign;

class InformationReservationController extends Controller
{

      public function add(Request $request)
      {
         // return  $request->all();jsCode
            $informationReservation = new InformationReservation();
            $code = $request->input('jsCode');
            $miniProgram = \EasyWeChat::miniProgram();
            $app = $miniProgram->auth->session($code);
            $openid = $app['openid'];
            // $openid = $request->input('openid');
            $user = User::where('openid', $openid)->get()->toArray();
            $user_id =$user[0]['id'];
            // $user_id = 1;
            $name = $request->input('name');
            $phone_number = $request->input('phone_number');
            // $service_time = $request->input('service_time');
            $content = $request->input('content');
            $user_id = $user_id;

            $informationReservation->name = $name;
            $informationReservation->phone_number = $phone_number;
            $informationReservation->content = $content;
            $informationReservation->user_id = $user_id;
            if($informationReservation->save()){
              return "资讯记录ok";
            }
      }

      public function list(Request $request)
      {
        $code = $request->input('jsCode');
        $miniProgram = \EasyWeChat::miniProgram();
        $app = $miniProgram->auth->session($code);
        $openid = $app['openid'];
        // $openid = $request->input('openid');
        $user = User::where('openid', $openid)->get()->toArray();
        $user_id =$user[0]['id'];
           // $user_id = 1;
           $list = InformationReservation::where('user_id', $user_id)->paginate(10);
           return $list;
      }
}
